import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const nav = {
  state: {
    options: [
      {
        icon: 'el-icon-menu',
        name: '首页',
        to: '/index/index'
      },
      {
        icon: 'el-icon-menu',
        name: '账单管理',
        to: '/statement/list'
      },
      {
        icon: 'el-icon-menu',
        name: '分类管理',
        to: '/classify/list'
      }
    ]
  },
  mutations: {},
  actions: {},
  getters: {}
}

const tabs = {
  state: {
    options: [],
    activeIndex: ''
  },
  mutations: {
    // 添加tabs
    addTabs (state, data) {
      console.log(state, data)
      let flag = false
      for (let option of state.options) {
        if (option.name === data.name && option.to === data.to) {
          flag = true
        }
      }
      if (!flag) {
        state.options.push(data)
      }
    },
    // 删除tabs
    deleteTabs (state, to) {
      let index = 0
      for (let option of state.options) {
        if (option.to === to) {
          break
        }
        index++
      }
      state.options.splice(index, 1)
    },
    // 设置当前激活的tab
    setActiveIndex (state, index) {
      state.activeIndex = index
    },
    // 移除所有tabs
    removeAllTabes (state) {
      state.options = []
    }
  },
  actions: {},
  getters: {}
}

const store = new Vuex.Store({
  modules: {
    nav: nav,
    tabs: tabs
  }
})

export default store
